//
//  XLLoginViewController.swift
//  LaowuquanS
//
//  Created by 王小林 on 26/9/18.
//  Copyright © 2018 王小林. All rights reserved.
//

import UIKit

class XLLoginViewController: UIViewController {
    
    //定义
    let margin = 30,padding = 20
    var _timer :Timer!
    var _timerNumber:Int = 60
    
//    let userTextFild :UITextField = {
//        let textFild = UITextField()
//        textFild.placeholder="请输入登陆手机号"
//        textFild.keyboardType = UIKeyboardType.numberPad
//        textFild.borderStyle = UITextField.BorderStyle.none
//        return textFild
//    }()
    
    var userTextFild : UITextField = {
        let textFild = UITextField()
        textFild.placeholder="请输入登陆手机号"
        textFild.keyboardType = UIKeyboardType.numberPad
        textFild.borderStyle = UITextField.BorderStyle.none
        return textFild
    }()
    
    let passwordTextFild :UITextField = {
        let textFild = UITextField()
        textFild.placeholder="请输入验证码"
        textFild.keyboardType = UIKeyboardType.numberPad
        textFild.borderStyle = UITextField.BorderStyle.none
        return textFild
    }()
    
    let codeBtn :UIButton = {
        let btn = UIButton.init(type: UIButton.ButtonType.custom)
        btn.setTitle("获取", for: UIControl.State.normal)
        btn.setTitleColor(kXLNavBarColor, for: UIControl.State.normal)
        btn.titleLabel?.font=UIFont.systemFont(ofSize: 17)
        btn.addTarget(self, action: #selector(codeBtnClick(btn:)),  for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    let loginBtn :UIButton = {
        let btn = UIButton.init(type: UIButton.ButtonType.custom)
        btn.setTitle("确认登陆", for: UIControl.State.normal)
        btn.backgroundColor=kXLNavBarColor
        btn.titleLabel?.font=UIFont.systemFont(ofSize: 17)
        btn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        viewBorderRadius(view: btn, radius: 4, width: 0, color: kHCNavBarColor)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        layoutView()
    }
    //MARK:- 布局
    func layoutView() -> Void {
        
        view.backgroundColor=UIColor.white
        //背景图
        let backgroundImage = UIImageView.init(image: UIImage.init(named: "login_bg"))
        view .addSubview(backgroundImage)
        backgroundImage.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: SCREEN_WIDTH, height: SCREEN_WIDTH/375*291))
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalTo(view.snp.top)
        }
        //logo
        let logoImage = UIImageView.init(image: UIImage.init(named: "login_logo"))
        view .addSubview(logoImage)
        logoImage.snp.makeConstraints { (make) in
            make.width.height.equalTo(60)
            make.centerX.equalTo(view.snp.centerX)
            make.bottom.equalTo(backgroundImage.snp.bottom).offset(-80)
        }
        //账号
        let userImage:UIImageView = {
            let imageView = UIImageView.init(image: UIImage.init(named: "login_user"))
            return imageView
        }()
        view.addSubview(userImage)
        userImage.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left).offset(margin)
            make.top.equalTo(backgroundImage.snp.bottom).offset(50)
            make.width.height.equalTo(padding)
        }
        
        view.addSubview(userTextFild)
        userTextFild.snp.makeConstraints { (make) in
            make.left.equalTo(userImage.snp.right).offset(padding)
            make.right.equalTo(view.snp.right).offset(-margin)
            make.top.equalTo(userImage.snp.top)
            make.height.equalTo(padding)
        }
        //密码
        
        let passwordImage:UIImageView = {
            let imageView = UIImageView.init(image: UIImage.init(named: "login_password"))
            return imageView
        }()
        view.addSubview(passwordImage)
        passwordImage.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left).offset(margin)
            make.top.equalTo(userImage.snp.bottom).offset(margin)
            make.width.height.equalTo(padding)
        }
        //验证码按钮
        view.addSubview(codeBtn)
        codeBtn.snp.makeConstraints { (make) in
            make.right.equalTo(view.snp.right).offset(-margin)
            make.centerY.equalTo(passwordImage.snp.centerY)
            make.height.equalTo(margin)
            make.width.equalTo(80)
        }
        
        view.addSubview(passwordTextFild)
        passwordTextFild.snp.makeConstraints { (make) in
            make.left.equalTo(passwordImage.snp.right).offset(padding)
            make.right.equalTo(codeBtn.snp.left).offset(-padding)
            make.centerY.equalTo(passwordImage.snp.centerY)
            make.height.equalTo(margin)
        }
        
        //登陆按钮
        view.addSubview(loginBtn)
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left).offset(margin)
            make.right.equalTo(view.snp.right).offset(-margin)
            make.height.equalTo(50)
            make.top.equalTo(passwordImage.snp.bottom).offset(50)
        }
    }
    //申请验证码
    @objc func codeBtnClick(btn:UIButton) -> Void {
        if  userTextFild.text?.count==0 {
            self.noticeOnlyText("手机号为空!")
            return
        }else if !XLUtils.checkPhoneNum(num:userTextFild.text ?? "")
        {
            self.noticeOnlyText("手机号有误,请重新填写!")
            return
        }
        startTimer()
        self.requestForgetCode()
    }
    //登录按钮
    @objc func loginBtnClick() -> Void {
        
    }
    //倒计时
    func startTimer() -> Void {
        if  _timer != nil  {
            _timer.invalidate()
            _timer = nil
        }
        _timerNumber=60
        _timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handkeTimerAction), userInfo: nil, repeats:true)
    }
    //倒计时的方法
    @objc func handkeTimerAction() -> Void {
        codeBtn.isEnabled=false
        _timerNumber-=1
        codeBtn.setTitle(String(format: "%d秒", (_timerNumber)), for: .normal)
        if _timerNumber==0 {
            _timer.invalidate()
            _timer=nil
            codeBtn.setTitle("重新获取", for: .normal)
            codeBtn.isEnabled=true
        }
    }
    //MARK:- 网络请求
    //请求验证码
    func requestForgetCode() -> Void {
        XLNetworkTools.requestData(.get, URLString: "user/smscode", parameters: ["phone":userTextFild.text!]) { (_ status: XLRequestStatus, _ result, _ tipString) in
            switch status{
            case .XLRequestStatusSuccess:
                self.noticeOnlyText("获取成功")
                break
            default:
                self.noticeOnlyText(tipString ?? "请求失败")
                break
            }
        }
    }
    //登录
    func requestForLogin() -> Void {
        XLNetworkTools.requestData(.get, URLString: "user/login", parameters: ["phone":userTextFild.text!,"code":passwordTextFild.text!]) { (status, result, tipString) in
            switch status{
            case .XLRequestStatusSuccess:
                
                
                
                break
            default:
                self.noticeOnlyText(tipString ?? "请求失败")
                break
            }
        }
    }
    //点击收键盘
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        userTextFild.resignFirstResponder();
        passwordTextFild.resignFirstResponder();
    }
    
    
}
